package univer.oko.payment.monthly;

import univer.oko.utilities.calendar.Year;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * @author Eduard Dudar
 *         Date: 21.08.2005
 *         Time: 1:25:01
 */
public class PaymentMonthlyList implements Serializable {
    private LinkedList years = new LinkedList();

    public PaymentMonthlyList() {

    }

    public PaymentMonthlyList(int firstYear, int initMonth) {
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH);

        firstYear = initMonth > Calendar.JUNE ? firstYear : firstYear - 1;
        int lastYear = currentMonth > Calendar.JUNE ? currentYear : currentYear - 1;

        for (int i = firstYear, j = 0; i <= lastYear; i++, j++) {
            this.years.add(new Year(i));
        }
    }

    public boolean isEmpty() {
        return this.years.isEmpty();
    }

    public LinkedList getYears() {
        return this.years;
    }

    public void add(int year, int month, double grn, double usd) {
        Year y = this.getYear(month > Calendar.JUNE ? year : year - 1);
        if (y != null) {
            y.setPayment(month, grn, usd);
        }
    }

    private Year getYear(int year) {
        for (Iterator i = this.years.iterator(); i.hasNext();) {
            Year y = (Year) i.next();
            if (y.getNumber() == year) {
                return y;
            }
        }
        return null;
    }

    public void setEntry(int year, double grn, double usd) {
        Year y = this.getYear(year);
        if (y != null) {
            y.setEntry(grn, usd);
        }
    }
}
